package com.abso.mp3tunes.locker.ui.views;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.jface.resource.JFaceColors;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.dialogs.PreferencesUtil;
import org.eclipse.ui.forms.events.HyperlinkAdapter;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.widgets.Hyperlink;

import com.abso.mp3tunes.locker.ui.ILockerConstants;
import com.abso.mp3tunes.locker.ui.LockerPlugin;
import com.abso.mp3tunes.locker.ui.preferences.LockerPreferencePage;
import com.abso.mp3tunes.locker.ui.viewers.LockerViewer;

/**
 * A specialized panel shown when the Locker browser cannot connect to the
 * Locker because the login data is not set in the Locker preferences.
 */
public class UnconfiguredViewPanel extends LockerBrowserViewPanel {

	/**
	 * Constructs a new panel.
	 * 
	 * @param parent
	 *            the parent control.
	 * @param lockerBrowserView
	 *            the locker browser view.
	 */
	public UnconfiguredViewPanel(Composite parent,
			LockerBrowserView lockerBrowserView) {
		super(parent, lockerBrowserView);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.abso.mp3tunes.locker.ui.views.LockerBrowserViewPanel#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public Composite createControl(final Composite parent) {
		Composite mainComp = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout(1, true);
		layout.marginWidth = 2;
		layout.marginHeight = 2;
		mainComp.setLayout(layout);

		/* adds the warning label */
		Label label = new Label(mainComp, SWT.WRAP);
		label.setText("MP3tunes Locker e-mail and password not configured.");
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		/* adds the hyperlink */
		Hyperlink link = new Hyperlink(mainComp, SWT.NONE);
		link.setText("Configure...");
		link.setUnderlined(true);
		link.setForeground(JFaceColors.getActiveHyperlinkText(parent
				.getDisplay()));
		link.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		link.addHyperlinkListener(new HyperlinkAdapter() {
			public void linkActivated(HyperlinkEvent e) {
				PreferenceDialog dialog = PreferencesUtil
						.createPreferenceDialogOn(parent.getShell(),
								LockerPreferencePage.class.getName(), null,
								null);
				if (dialog.open() == Window.OK) {
					if (!StringUtils.isEmpty(LockerPlugin.getDefault()
							.getPreferenceStore().getString(ILockerConstants.PREF_USERNAME))) {
						getLockerBrowserView().setViewMode(
								LockerBrowserView.ARTIST_MODE);
					}
				}
			}
		});
		return mainComp;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.abso.mp3tunes.locker.ui.views.LockerBrowserViewPanel#getLockerViewer()
	 */
	protected LockerViewer getLockerViewer() {
		return null;
	}

}
